const fileCache = require('think-cache-file');
const redisCache = require('think-cache-redis');
const nunjucks = require('think-view-nunjucks');
const fileSession = require('think-session-file');
const mysql = require('think-model-mysql');
const {Console, File, DateFile} = require('think-logger3');
const socketio = require('think-websocket-socket.io');
const ws = require('think-websocket-ws');
const path = require('path');
const isDev = think.env === 'development';

/**
 * cache adapter config
 * @type {Object}
 */
exports.cache = {
	type: 'file',
	common: {
		timeout: 24 * 60 * 60 * 1000 // millisecond
	},
	file: {
		handle: fileCache,
		cachePath: path.join(think.ROOT_PATH, 'runtime/cache'), // absoulte path is necessarily required
		pathDepth: 1,
		gcInterval: 24 * 60 * 60 * 1000 // gc interval
	}
};

/**
 * model adapter config
 * @type {Object}
 */
/*exports.model = {
	type: 'mysql',
	common: {
		logConnect: isDev,
		logSql: isDev,
		logger: msg => think.logger.info(msg)
	},
	mysql: {
		handle: mysql,
		database: '',
		prefix: 'think_',
		encoding: 'utf8',
		host: '127.0.0.1',
		port: '',
		user: 'root',
		password: 'root',
		dateStrings: true,
		connectionLimit: 5
	}
};*/

/**
 * @author: ares
 * @date: 2020/3/8 8:43
 * @description: mongo model adapter config
 */
/*exports.model = {
	type: 'mongo',
	common: {
		logConnect: true,
		logger: msg => think.logger.info(msg)
	},
	mongo: {
		host: '127.0.0.1',
		port: 27017,
		user: 'ares',
		password: 'ares',
		database: 'test',
		options: {
			// 身份验证相关
			// replicaSet: '',
			// authSource: ''
		}
	}
};*/

/**
 * @author: ares
 * @date: 2020/3/8 8:44
 * @description: mongoose model adapter config
 */
exports.model = {
	type: 'mongoose',
	common: {
		logConnect: true,
		logger: msg => think.logger.info(msg)
	},
	mongoose: {
		host: '127.0.0.1',
		port: 27017,
		user: 'ares',
		password: 'ares',
		database: 'test',
		useCollectionPlural: false,
		options: {}
	}
};

/**
 * session adapter config
 * @type {Object}
 */
exports.session = {
	type: 'file',
	common: {
		cookie: {
			name: 'thinkjs'
			// keys: ['werwer', 'werwer'],
			// signed: true
		}
	},
	file: {
		handle: fileSession,
		sessionPath: path.join(think.ROOT_PATH, 'runtime/session')
	}
};

/**
 * view adapter config
 * @type {Object}
 */
exports.view = {
	type: 'nunjucks',
	common: {
		viewPath: path.join(think.ROOT_PATH, 'view'),
		sep: '_',
		extname: '.html'
	},
	nunjucks: {
		handle: nunjucks
	}
};

/**
 * logger adapter config
 * @type {Object}
 */
exports.logger = {
	type: isDev ? 'console' : 'dateFile',
	console: {
		handle: Console
	},
	file: {
		handle: File,
		backups: 10, // max chunk number
		absolute: true,
		maxLogSize: 50 * 1024, // 50M
		filename: path.join(think.ROOT_PATH, 'logs/app.log')
	},
	dateFile: {
		handle: DateFile,
		level: 'ALL',
		absolute: true,
		pattern: '-yyyy-MM-dd',
		alwaysIncludePattern: true,
		filename: path.join(think.ROOT_PATH, 'logs/app.log')
	}
};

/**
 * @author: ares
 * @date: 2020/3/8 11:51
 * @description: socketio adapter config
 */
exports.websocket = {
	type: 'socketio',
	common: {
		// common config
	},
	socketio: {
		handle: socketio,
		pingInterval: 10000,
		pingTimeout: 5000,
		serveClient:false,
		allowOrigin: null,
		path: '/socket.io',
		adapter: null,
		messages: [{
			namespace:'/',
			open: '/websocketio/open',
			message: '/websocketio/message',
			close: '/websocketio/close'
		}]
	}
}

/**
 * @author: ares
 * @date: 2020/3/8 11:51
 * @description: ws adapter config
 */
/*exports.websocket = {
	type: 'ws',
	common: {
		// common config
	},
	ws: {
		handle: ws,
		pingInterval: 10000,
	    pingTimeout: 5000,
	    serveClient:false,
		allowOrigin: null,
		path: '/ws',
		adapter: null,
		messages: [{
			open: '/websocket/open',
			message: '/websocket/message',
			close: '/websocket/close'
		}]
	}
}*/
